package adminorg;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TreeMap;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.Link;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.themes.Runo;

public class StatisticDetailTab extends VerticalLayout{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4683036638374032254L;
	private Object statUseId;
	private Table detailStatTable = null;
	private PeriodHelper PHelp = null;
	private DatabaseHelper dbHelp = null;
	private StatisticDataHelper SDHelp = null;
	private String PT = null;
	private HorizontalSplitPanel StatData = null;
	private Panel ChartPanel = null;
	private Date LastDate = null;
	
	private Command SCommand = new Command() {
		/**
		 * 
		 */
		private static final long serialVersionUID = 7957973184773797520L;

		@Override
		public void menuSelected(MenuItem selectedItem) {
			// TODO Auto-generated method stub
			if (selectedItem.getText().equals(dbHelp.getWords("Edit"))) {
				setNotReadOnly();
				selectedItem.setText(dbHelp.getWords("OK"));
			} else if (selectedItem.getText().equals(dbHelp.getWords("OK"))){
				for (Object ID:detailStatTable.getItemIds()){
					SDHelp.SaveDate(PHelp.TruncDate((Date)detailStatTable.getContainerProperty(ID, "date").getValue()), statUseId, 
								Double.parseDouble(((TextField)detailStatTable.getContainerProperty(ID, "sum").getValue()).getValue().toString()));
				}
				selectedItem.setText(dbHelp.getWords("Edit"));
				setReadOnly();
				repaintChart();
			} else if (selectedItem.getText().equals(dbHelp.getWords("OnlyStat"))){
				StatData.setVisible(false);
				selectedItem.setText(dbHelp.getWords("ShowData"));
			} else if (selectedItem.getText().equals(dbHelp.getWords("ShowData"))){
				StatData.setVisible(true);
				selectedItem.setText(dbHelp.getWords("OnlyStat"));
			}
		}
		
	};
	public StatisticDetailTab(DatabaseHelper dbHelper, Object statUseId, Date LastDate, PeriodHelper PHelp) throws Exception{
		this();
		this.statUseId=statUseId;
		//this.setDescription(statUseId.toString());
		this.setWidth("100%");
		this.dbHelp = dbHelper;
		this.SDHelp = new StatisticDataHelper(dbHelper);
		this.PHelp = PHelp;
		this.LastDate = LastDate;
		StatData = new HorizontalSplitPanel();
		StatData.setWidth("100%");
		Panel DetailPanel = new Panel();
		DetailPanel.setWidth("100%");
		VerticalLayout Info = new VerticalLayout();
		Info.setWidth("100%");
		//NAME
		Label StatName = new Label("<h2>".concat(
				dbHelp.views.getStatisticContainer().getContainerProperty(statUseId, 
						"dictstatistic.name").getValue().toString()).concat("</h2>"));
		StatName.setContentMode(Label.CONTENT_XHTML);
		Info.addComponent(StatName);
		Info.setComponentAlignment(StatName, Alignment.TOP_CENTER);
		//CODE
		Label StatCode = new Label(	"<b>".concat(dbHelp.getWords("Code")).concat(": </b>").concat(
				dbHelp.views.getStatisticContainer().getContainerProperty(statUseId, 
				"dictstatistic.code").getValue().toString()));
		StatCode.setContentMode(Label.CONTENT_XHTML);
		Info.addComponent(StatCode);
		Info.setComponentAlignment(StatCode, Alignment.TOP_LEFT);
		//DESCRIPTION
		Label StatDescr = new Label("<b>".concat(dbHelp.getWords("Description")).concat(": </b><br>").concat(dbHelp.views.getStatisticContainer().getContainerProperty(statUseId, 
				"dictstatistic.description").getValue().toString()));
		StatDescr.setContentMode(Label.CONTENT_XHTML);
		Info.addComponent(StatDescr);
		Info.setComponentAlignment(StatDescr, Alignment.TOP_LEFT);
		Link DocLink = new Link(dbHelp.getWords("DetailedDescription"),new ExternalResource(dbHelp.views.getStatisticContainer().getContainerProperty(statUseId,
				"dictstatistic.docs").getValue().toString()));
		Info.addComponent(DocLink);
		Info.setComponentAlignment(DocLink, Alignment.TOP_LEFT);
		//DOOBLE
		Label StatDouble = new Label("<b>".concat(dbHelp.getWords("Double")).concat(": </b><br>").concat(dbHelp.views.getStatisticContainer().getContainerProperty(statUseId, 
				"dictstatistic.dooble").getValue().toString()));
		StatDouble.setContentMode(Label.CONTENT_XHTML);
		Info.addComponent(StatDouble);
		Info.setComponentAlignment(StatDouble, Alignment.TOP_LEFT);
		//CLAC
		Label StatCalc = new Label("<b>".concat(dbHelp.getWords("Calc")).concat(": </b><br>").concat(dbHelp.views.getStatisticContainer().getContainerProperty(statUseId, 
				"dictstatistic.calc").getValue().toString()));
		StatCalc.setContentMode(Label.CONTENT_XHTML);
		Info.addComponent(StatCalc);
		Info.setComponentAlignment(StatCalc, Alignment.TOP_LEFT);
		//POST
		Label StatPost = new Label("<b>".concat(dbHelp.getWords("Post")).concat(": </b>").concat(dbHelp.views.getStatisticContainer().getContainerProperty(statUseId, 
				"orgschema.name").getValue().toString()));
		StatPost.setContentMode(Label.CONTENT_XHTML);
		Info.addComponent(StatPost);
		Info.setComponentAlignment(StatPost, Alignment.TOP_LEFT);
		//DEPARTMANT
		Label StatDept = new Label("<b>".concat(dbHelp.getWords("Department")).concat(": </b>").concat(dbHelp.views.getStatisticContainer().getContainerProperty(statUseId, 
				"orgschema.dept_name").getValue().toString()));
		StatDept.setContentMode(Label.CONTENT_XHTML);
		Info.addComponent(StatDept);
		Info.setComponentAlignment(StatDept, Alignment.TOP_LEFT);
		//EMPLOEE
		Label StatEmp = new Label("<b>".concat(dbHelp.getWords("Employee")).concat(": </b>").concat(dbHelp.views.getStatisticContainer().getContainerProperty(statUseId, 
				"compacc.name").getValue().toString()));
		StatEmp.setContentMode(Label.CONTENT_XHTML);
		Info.addComponent(StatEmp);
		Info.setComponentAlignment(StatEmp, Alignment.TOP_LEFT);
		
		DetailPanel.setContent(Info);

		//DETAIL DATA
		Panel DataPanel = new Panel();
		DataPanel.setWidth("100%");
		detailStatTable = new Table();
		detailStatTable.addContainerProperty("date", Date.class, null);
		detailStatTable.addContainerProperty("period", String.class, "");
		detailStatTable.addContainerProperty("sum", TextField.class, null);
		PT = new String(dbHelp.views.getStatisticContainer().getContainerProperty(statUseId,
				"aperiodprops.type").getValue().toString());
		SimpleDateFormat df =
			    new SimpleDateFormat("dd.MM.yyyy");
		if (PT.equals("DAILY")){
			TreeMap<Integer,Date> DateValues = new TreeMap<Integer,Date>();
			DateValues = PHelp.getLastPeriodsD(LastDate);
			for (int i=PHelp.getStatQuant();i>(PHelp.getStatQuant()-DateValues.size());i--){
				TextField TF = new TextField();
				TF.setValue(SDHelp.getDataD(DateValues.get(i), statUseId));
				detailStatTable.addItem(new Object[]{DateValues.get(i), new String(df.format(DateValues.get(i))),
						TF}, new Integer(i));
				TF.setReadOnly(true);
			} 
		} else {
			TreeMap<Integer,Period> PeriodValues = new TreeMap<Integer,Period>();
			PeriodValues = PHelp.getLastPeriods(LastDate);
			for (int i=PHelp.getStatQuant();i>(PHelp.getStatQuant()-PeriodValues.size());i--){
				TextField TF = new TextField();
				TF.setValue(SDHelp.getDataD(PeriodValues.get(i).getStartDate(), statUseId));
				if (PHelp.getPeriodView()){
					detailStatTable.addItem(new Object[]{PeriodValues.get(i).getStartDate(), new String(PeriodValues.get(i).getStringValue()),
							TF}, new Integer(i));
				} else {detailStatTable.addItem(new Object[]{PeriodValues.get(i).getStartDate(), new String(String.valueOf(PeriodValues.get(i).getNumber())),
							TF}, new Integer(i));
				}
				TF.setReadOnly(true);
			}  
		}
		detailStatTable.setColumnCollapsingAllowed(true);
		detailStatTable.setColumnReorderingAllowed(true);
		detailStatTable.setSelectable(true);
		detailStatTable.setStyleName(Runo.TABLE_SMALL);
		detailStatTable.setVisibleColumns(new Object[]{"period","sum"});
		detailStatTable.setColumnHeaders(new String[]{dbHelp.getWords("Periods"),dbHelp.getWords("Values")});
		detailStatTable.setImmediate(true);
		VerticalLayout L = new VerticalLayout();

		L.addComponent(detailStatTable);
		DataPanel.setContent(L);
		L.setWidth("100%");	
		detailStatTable.setWidth("100%");
		StatData.setFirstComponent(DataPanel);
		StatData.setSecondComponent(DetailPanel);
		StatData.setSplitPosition(40);
		//MENU
		MenuBar SBar = new MenuBar();
		SBar.addItem(dbHelp.getWords("Edit"), SCommand);
		SBar.addItem(dbHelp.getWords("OnlyStat"), SCommand);
		this.addComponent(SBar);
		this.addComponent(StatData);
		//CHART
		ChartPanel = new Panel();


		PaintStatisticChart LC;
		try {
			LC = new PaintStatisticChart(dbHelp, statUseId, LastDate,PHelp);
			ChartPanel.setContent(LC);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		this.addComponent(ChartPanel);
	}
	
	public StatisticDetailTab() {
		// TODO Auto-generated constructor stub
	}
	
	public Object getStatUseId(){
		return statUseId;
	}
	
	public void setNotReadOnly(){
		for (Object ID:detailStatTable.getItemIds()){
			((TextField)detailStatTable.getContainerProperty(ID, "sum").getValue()).setReadOnly(false);
		}
	}
	
	public void setReadOnly(){
		for (Object ID:detailStatTable.getItemIds()){
			((TextField)detailStatTable.getContainerProperty(ID, "sum").getValue()).setReadOnly(true);
		}
	}
	
	
	public void repaintChart(){
		ChartPanel.removeAllComponents();
		PaintStatisticChart LC;
		try {
			LC = new PaintStatisticChart(dbHelp, statUseId, LastDate,PHelp);
			ChartPanel.setContent(LC);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
